package com.zhuangfei.hpuhelper.fragment;

import android.os.Bundle;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.hpu.bean.FailScore;
import com.hpu.bean.Score;
import com.hpu.constants.HttpConstants;
import com.hpu.constants.ShareConstants;
import com.hpu.parser.BodyScoreParser;
import com.hpu.parser.FailScoreParser;
import com.hpu.service.BodyScoreService;
import com.hpu.service.FailScoreService;
import com.hpu.service.LoginService;
import com.hpu.tools.ShareTools;
import com.zhuangfei.hpuhelper.tools.DefaultHandler;
import com.zhuangfei.hpuhelper.view.MyApplication;
import com.zhuangfei.smartalert.core.MessageAlert;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.lzf.serviceplatform.R;

public class BodyScoreFragment extends LazyLoadFragment{

	/**
	 * 复用 view
	 */
	private View mView;

	/**
	 * 列表相关
	 */
	List<Map<String, String>> items = new ArrayList<>();
	SimpleAdapter adapter;

	private ListView mListView;

	/**
	 * 业务类
	 */
	private BodyScoreService bodyScoreService;
	private LoginService loginService;

	private MessageAlert messageAlert;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.fragment_body_score, container, false);
		return mView;
	}

	@Override
	public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
		initView();
		initEvent();
		super.onViewCreated(view, savedInstanceState);
	}

	@Override
	protected void lazyLoad() {
		bodyScoreService.getBodyScore();
	}

	private void initView() {
		messageAlert=new MessageAlert(getActivity()).create();
		
		// 业务逻辑对象
		bodyScoreService = new BodyScoreService(handler, getActivity());
		loginService = new LoginService(getActivity(),handler);

		// 刷新控件初始化
		mListView= (ListView) mView.findViewById(R.id.id_body_score_listview);

		adapter = new SimpleAdapter(getActivity(), items, R.layout.item_body_score_layout,
				new String[] {"value"},
				new int[] { R.id.id_body_score });
		mListView.setAdapter(adapter);
	}
	
	private void initEvent() {
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				
//				Map map=items.get(arg2);
//				String content=map.get("subject")+"\n课程号："+map.get("number")+"\n课序号："+
//				map.get("order")+"\n学分："+map.get("credit")+"\n成绩："+map.get("value");
//				messageAlert.setTitle("不及格成绩详情")
//				.setContent(content)
//				.show();
			}
			
		});
	}

	protected void showResult(List<String> list) {
		if(list==null) return;
		items.clear();

		if(list.size()==0){
			getViewHelper().showEmptyView("没有体测成绩");
		}
		for(int i=0;i<list.size();i++){
			String str=list.get(i);
			Map<String,String> map=new HashMap<>();
			map.put("value",str);
			items.add(map);
		}

		adapter.notifyDataSetChanged();
	}

	DefaultHandler handler = new DefaultHandler(MyApplication.getInstance()) {
		public void handleMessage(Message msg) {
			getViewHelper().showSuccessView();
			super.handleMessage(msg);
			
			switch (msg.what) {
			// 成绩获取
			case HttpConstants.QUERY_BODY_SCORE_SUCCESS:
				Log.i("BODY",msg.obj.toString());
				BodyScoreParser bodyScoreParser=new BodyScoreParser();
				List<String> list=bodyScoreParser.parse(msg.obj.toString());
				showResult(list);
				break;

			// VPN过期，重新登录
			case HttpConstants.VPN_TOOLONG:
				if(handler.isIgnoreVpnExpired()){
					break;
				}
				getViewHelper().showLoadingView();
				loginService.login(ShareTools.get(getActivity(), ShareConstants.STORE_USER, "").toString(),
						ShareTools.get(getActivity(),ShareConstants.STORE_PASSWORD1, "").toString(),
						ShareTools.get(getActivity(), ShareConstants.STORE_PASSWORD2, "").toString());
				break;

			// 登录成功,重新获取
			case HttpConstants.LOGIN_SUCCESS:
				getViewHelper().showLoadingView();
				ShareTools.put(getActivity(), ShareConstants.STORE_LOGINCOOKIE, (String) msg.obj);
				bodyScoreService.getBodyScore();
				break;
			}
		};
	};

	@Override
	public void onLoading() {
		super.onLoading();
		getViewHelper().showLoadingView();
		bodyScoreService.getBodyScore();
	}
}
